//
//  SDCacheManagement.m
//  SKinDiary
//
//  Created by pcyang on 16/6/26.
//  Copyright © 2016年 tencent. All rights reserved.
//

#import "SDCacheManagement.h"

#import <objc/runtime.h>
#import "SDImageCacheEx.h"

NSString* Notification_CacheWillbeCleaned = @"Notification_CacheWillbeCleaned";
//#define kFileCleanerMaxExpiredTime (10 * 24 * 60 * 60)
//#define kMatchFileCleanerMaxExpiredTime (10 * 24 * 60 * 60)

//#define kFileCleanerMaxExpiredTime 1

@implementation SDCacheManagement

+ (void)cleanAllCaches:(BOOL)userChanged completion:(void (^)(void))completion
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),
                   ^{
                       Class curClass = NSClassFromString(@"SDImageLocalCacheManagement");
//                       if( class_conformsToProtocolEx(curClass,@protocol(SDCleanCacheProtocol)) && class_getClassMethod(curClass,@selector(cleanCache)) )
//                       {
//                           [curClass cleanCache];
//                       }
                       
                       [SDFile deleteDictionary:[SDFile getCacheDirectory]];
                       
                       if(completion)
                       {
                           dispatch_async(dispatch_get_main_queue(), ^{
                               completion();
                           });
                       }
                   });
}

+ (void)cleanAllCachesWithUserChanged:(BOOL)userChanged completion:(void (^)(void))completion
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),^{
        NSDate* date = [NSDate date];
        dispatch_group_t group = dispatch_group_create();
        dispatch_group_async(group, dispatch_get_main_queue(), ^{
            [[NSNotificationCenter defaultCenter]postNotificationName:Notification_CacheWillbeCleaned object:nil];
        });
        
        //        unsigned int count = 0;
        //        Class* classes = objc_copyClassList(&count);
        
        NSString *rootPath = [self rootCachePath];
        NSString *imgPath = [SDFile getDirectoryInCacheWithName:@"ImageLocalCacheManagement"];
        
        dispatch_sync(dispatch_get_main_queue(),^{
            
            [SDFile deleteDictionary:rootPath];
            [SDFile deleteDictionary:imgPath];
            //                    for (int i=0; i<count; i++)
            //                    {
            //                        Class curClass = classes[i];
            //                        if( class_conformsToProtocolEx(curClass,@protocol(VPCleanCacheProtocol)) && class_getClassMethod(curClass,@selector(cleanCache)) )
            //                        {
            //                            if(!userChanged)
            //                                [curClass cleanCache];
            //                            else
            //                            {
            //                                if( class_getClassMethod(curClass,@selector(cleanCacheWhenUserChanged)) && [curClass cleanCacheWhenUserChanged] )
            //                                    [curClass cleanCache];
            //                            }
            //                        }
            //                    }
        });
        
        dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
       // dispatch_release(group);
        
        NSTimeInterval timeInterval = [[NSDate date]timeIntervalSinceDate:date];
        
        //        free(classes);
        
        [NSThread sleepForTimeInterval:timeInterval<0.5?0.5-timeInterval:0];
        if(completion)
            dispatch_async(dispatch_get_main_queue(), ^{
                completion();
            });
    });
}

+ (void)cleanAllDataCaches
{
    [SDFile deleteDictionary:[self rootCachePath]];
}

+ (NSString*)rootCachePath
{
    return [SDFile getDirectoryInCacheWithName:@"com.tencent.skindiary.cache"];
}

+ (NSString*)rootDocumentPath
{
    return [SDFile getDirectoryInDocumentWithName:@"com.tencent.skindiary.document"];
}

+ (NSString*)getFilePathInCache:(NSString *)filename
{
    NSString* path = [[self rootCachePath]stringByAppendingPathComponent:filename];
    [SDFile checkDirectoryPath:path];
    return path;
}

+ (NSString*)getFilePathInDocument:(NSString *)filename
{
    NSString* path = [[self rootDocumentPath]stringByAppendingPathComponent:filename];
    [SDFile checkDirectoryPath:path];
    return path;
}

+ (CGFloat)sizeOfCacheDic
{
    CGFloat catchSize = [SDFile folderSizeAtPath:[self rootCachePath]];
    NSString *path = [SDFile getDirectoryInCacheWithName:@"ImageLocalCacheManagement"];
    CGFloat imageSize = [SDFile folderSizeAtPath:path];
    return catchSize+imageSize;
}
@end

